package cn.wind.xboot.framework.utils;

import cn.wind.xboot.framework.enums.PermType;
import cn.wind.xboot.logic.sys.entity.SysPermission;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.List;

/**
 * <p>TODO
 *
 * @author xukk
 * @version 1.0
 */
public class MenuUtil {
    /**
     * xx
     * @param list
     * @return
     */
    @SneakyThrows
    public static List<SysPermission> tree(List<SysPermission> list){
            List<SysPermission> returnList = Lists.newArrayList();
            for (Iterator<SysPermission> iterator = list.iterator(); iterator.hasNext(); ) {
                SysPermission res = iterator.next();
                /**
                 * 判断第一个对象是否为第一个节点
                 *
                 */
                if ("0".equals(""+res.getPId())) {
                    /**
                     * 相等--说明第一个节点为父节点--递归下面的子节点
                     */
                    recursionFn(list, res);
                    returnList.add(res);
                }
            }
        return returnList;
    }

    /**
     * 递归列表
     * @param list
     * @param t
     * @param <T>
     */
    @SneakyThrows
    private static  void recursionFn(List<SysPermission> list,SysPermission t){
        List<SysPermission> childList = getChildList(list, t);
        List<String> buttonList=getButtonChildList(list,t);
        /**
         * 设置他的子集对象集
         */
        t.setChildren(childList);
        t.setButtonTypes(buttonList);
        /**
         * 迭代--这些子集的对象--时候还有下一级的子级对象
         */
        for (SysPermission nextChild : childList) {
            /**
             * 下一个对象，与所有的资源集进行判断
             */
            if (hasChild(list, nextChild)) {
                /**
                 * 有下一个子节点,递归
                 */
                Iterator<SysPermission> it = childList.iterator();
                while (it.hasNext()) {
                    SysPermission node = it.next();
                    /**
                     * 所有的对象--跟当前这个childList 的对象子节点
                     */
                    recursionFn(list, node);
                }
            }
        }
    }

    /**
     * 获得指定节点下的所有子节点
     * @param list
     * @param t
     * @param <T>
     * @return
     */
    @SneakyThrows
    private static   List<SysPermission> getChildList(List<SysPermission> list,SysPermission t) {
        List<SysPermission> childList =Lists.newArrayList();
        Iterator<SysPermission> it = list.iterator();
        while (it.hasNext()) {
            SysPermission child = it.next();
            /**
             * 判断集合的父ID是否等于上一级的id
             */
            if (PermType.MENU.getValue().equals(BeanUtils.getProperty(child,"type"))&&StringUtils.equals(BeanUtils.getProperty(child,"PId"),BeanUtils.getProperty(t,"id"))) {
                childList.add(child);
            }
        }
        return childList;
    }
    /**
     * 获得指定节点下的所有子节点
     * @param list
     * @param t
     * @param <T>
     * @return
     */
    @SneakyThrows
    private static   List<String> getButtonChildList(List<SysPermission> list,SysPermission t) {
        List<String> childList =Lists.newArrayList();
        Iterator<SysPermission> it = list.iterator();
        while (it.hasNext()) {
            SysPermission child = it.next();
            /**
             * 判断集合的父ID是否等于上一级的id
             */
            if (PermType.BUTTON.getValue().equals(BeanUtils.getProperty(child,"type"))&&StringUtils.equals(BeanUtils.getProperty(child,"PId"),BeanUtils.getProperty(t,"id"))) {
                childList.add(child.getButtonType());
            }
        }
        return childList;
    }
    /**
     * 判断是否还有下一个子节点
     * @param list
     * @param t
     * @param <T>
     * @return
     */
    @SneakyThrows
    private static  boolean hasChild(List<SysPermission> list, SysPermission t) {
        Iterator<SysPermission> it = list.iterator();
        while (it.hasNext()) {
            SysPermission child = it.next();
            /**
             * 判断集合的父ID是否等于上一级的id
             */
            if (PermType.MENU.getValue().equals(BeanUtils.getProperty(child,"type"))&&StringUtils.equals(BeanUtils.getProperty(child,"PId"),BeanUtils.getProperty(t,"id"))) {
               return true;
            }
        }
        return false;
    }
}
